package cn.maihe.elg.operation.service.system.impl;

import cn.maihe.elg.operation.exception.ErrorCode;
import cn.maihe.elg.operation.exception.SystemRuntimeException;
import cn.maihe.elg.operation.model.vo.Query;
import cn.maihe.elg.operation.repository.entity.OrgQualificationDO;
import cn.maihe.elg.operation.repository.mapper.OrgQualificationMapper;
import cn.maihe.elg.operation.service.system.OrgQualificationService;
import cn.maihe.elg.operation.service.system.UserService;
import cn.maihe.elg.operation.utils.IdGenerate;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author JHWei on 2018-07-13.
 * @ 创建日期：2018-07-13
 */
@Service
@Slf4j
public class OrgQualificationServiceImpl implements OrgQualificationService {

    @Resource
    private OrgQualificationMapper orgQualificationMapper;

    @Resource
    private UserService userService;



    /**
     * 新增机构资质
     *
     * @param orgQualification 机构资质信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createOrgQualification(OrgQualificationDO orgQualification) {
        //机构资质代码不允许重复。
        Integer codeOrg = orgQualificationMapper.selectCount(new EntityWrapper<OrgQualificationDO>()
                .eq("qualification_code", orgQualification.getQualificationCode()));
        if (codeOrg > 0) {
            throw new SystemRuntimeException(ErrorCode.BSMOM004);
        }
        orgQualification.setGmtCreate(new Date());
        orgQualification.setUserCreate(userService.getUserContext().getUsername());
        orgQualification.setGmtModified(new Date());
        orgQualification.setUserModified(userService.getUserContext().getUsername());
        orgQualification.setId(IdGenerate.getId());
        orgQualificationMapper.insert(orgQualification);
    }


    /**
     * 查询机构资质列表
     * //     * @param  机构Id
     * //     * @param  当前页码
     * //     * @param  页面大小
     *
     * @return 分页机构资质数据
     */
    @Override
    public PageInfo<OrgQualificationDO> findOrgQualificationByPage(Query obj) {
        try {
            obj.setPageNo(obj.getPageNo() == 0 ? 1 : obj.getPageNo());
            obj.setPageSize(obj.getPageSize() == 0 ? 10 : obj.getPageSize());
            PageHelper.startPage(obj.getPageNo(), obj.getPageSize());
            PageHelper.orderBy("gmt_modified desc");
            List<OrgQualificationDO> page = orgQualificationMapper.findByOrgId(obj.getParameters());
            return new PageInfo<>(page);
        } catch (Exception e) {
            log.error("机构资质数据查询报错:{}",e.getMessage(),e);
            throw new SystemRuntimeException(ErrorCode.BSMSC009);
        }

    }

    /**
     * 获取机构资质详情信息
     *
     * @param id 机构资质ID
     * @return id 对应机构资质
     */
    @Override
    public OrgQualificationDO getOrgQualification(Long id) {
        return orgQualificationMapper.selectById(id);
    }

    /**
     * 修改机构资质信息
     *
     * @param orgQualification 机构资质信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modifyOrgQualification(OrgQualificationDO orgQualification) {
        Long id = orgQualification.getId();
        if (id == null) {
            log.error("机构资质不存在或已被删除");
            throw new SystemRuntimeException(ErrorCode.BSMOM003);
        }
        OrgQualificationDO tempOrg = orgQualificationMapper.selectById(id);
        if (tempOrg == null) {
            log.error("机构资质不存在或已被删除");
            throw new SystemRuntimeException(ErrorCode.BSMOM003);
        }
        String code = orgQualification.getQualificationCode();
        //机构资质编号不允许重复。
        Integer nameOrg = orgQualificationMapper.selectCount(new EntityWrapper<OrgQualificationDO>()
                .eq("qualification_code", code)
                .ne("id", orgQualification.getId()));
        if (nameOrg > 0) {
            throw new SystemRuntimeException(ErrorCode.BSMOM004);
        }
        try {
            orgQualification.setGmtModified(new Date());
            orgQualification.setUserModified(userService.getUserContext().getUsername());
            // 目前机构资质编号是可以修改
            orgQualificationMapper.updateById(orgQualification);
        } catch (Exception e) {
            throw new SystemRuntimeException(ErrorCode.BSMSC007);
        }
    }


    /**
     * 删除机构资质
     *
     * @param id 机构资质ID
     */
    @Override
    public void deleteOrgQualification(Long id) {
        OrgQualificationDO orgQualification = orgQualificationMapper.selectById(id);
        if (orgQualification == null) {
            throw new SystemRuntimeException(ErrorCode.BSMCM001);
        }
        orgQualificationMapper.deleteById(id);
    }

}
